package com.apes.framework.util.monitor.druid.sql;

import com.apes.framework.plugin.cache.Cache;
import com.apes.framework.util.DateUtil;
import com.apes.framework.util.monitor.AbstractMonitorService;
import com.apes.framework.util.monitor.Constants;
import org.springframework.stereotype.Service;

@Service
public class SqlStackService extends AbstractMonitorService<SqlStack> {

    private SqlStackRepository repository;

    public SqlStackService(SqlStackRepository repository, Cache cache) {
        super(cache, null);
        this.repository = repository;
    }

    @Override
    public void run(SqlStack stack) {
        SqlStack sqlStack = repository.findById(stack.getId()).orElse(null);
        if (sqlStack == null) {
            sqlStack = stack;
        }
        sqlStack.incTimes();
        sqlStack.setEndedOn(DateUtil.format("yyyy-MM-dd'T'HH:mm:ss.SSS") + "+0800");
        repository.save(sqlStack);
    }

    @Override
    protected void deleteAllData() {
        repository.deleteAll();
    }

    @Override
    public String getTopic() {
        return Constants.CONST_DRUID_SQL_ENABLE;
    }
}
